package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.swing.JPanel;

import model.*;


/**
 * The class FieldView extends AbstractView
 * @author Arjan Frans, Alwin Rombout, Jelte Verbree, Brendan Kanters
 * @version 1.0
 */
public class FieldView extends AbstractView
{
	private static final long serialVersionUID = 7476638415910616650L;
	private final int GRID_VIEW_SCALING_FACTOR = 1;
	private static final Color EMPTY_COLOR =new Color(153,77,0);
    private int gridWidth, gridHeight;
    private int xScale, yScale;
    Dimension size;
    private Graphics g;
    private Image fieldImage;

    /**
     * Create a new FieldView component.
     */
    public FieldView(Simulator simulator)
    {
    	super(simulator);
    }
    
    /**
     * overrides the setSize method
     * @param width
     * @param height
     */
    @Override
    public void setSize(int width, int height)
    {
    	gridHeight = height;
    	gridWidth = width;
    }
    
    /**
     * Tell the GUI manager how big we would like to be.
     */
    public Dimension getPreferredSize()
    {
        return new Dimension(gridWidth,
                             gridHeight);
    }
    
    /**
     * Prepare for a new round of painting. Since the component
     * may be resized, compute the scaling factor again.
     */
    public void preparePaint()
    {
            fieldImage = this.createImage(gridWidth, gridHeight);
            g = fieldImage.getGraphics();

            xScale = gridWidth / simulator.getField().getWidth();
            if(xScale < 1) {
                xScale = GRID_VIEW_SCALING_FACTOR;
            }
            yScale = gridHeight / simulator.getField().getDepth();
            if(yScale < 1) {
                yScale = GRID_VIEW_SCALING_FACTOR;
            }
    }
    
    /**
     * overrides the updateView method
     */
    @Override
    public void updateView()
    {   
        this.preparePaint();
        Field field = simulator.getField();   
        for(int row = 0; row < field.getDepth(); row++) {
            for(int col = 0; col < field.getWidth(); col++) {
                Actor object = (Actor) field.getObjectAt(row, col);
                Grass grass = (Grass) field.getGrassAt(row,col);
                if(field.getRoadLocation(row, col) == true) {
                	this.drawMark(col, row, Color.gray);
                }
                if(object != null) {
                    this.drawMark(col, row, object.getColor());
                }
                else if( object == null && field.getRoadLocation(row, col) == false){
                	if(grass != null) {
                		this.drawMark(col, row, grass.getColor());
                	}
                	else {
                		this.drawMark(col, row, EMPTY_COLOR);
                	}
                }
            }
        }
        if(simulator.drawBomb() == true) AtomicBomb();
        this.repaint();
    }
    
    /**
     * Paint on grid location on this field in a given color.
     * @param x
     * @param y
     * @param color
     */
    public void drawMark(int x, int y, Color color)
    {
        g.setColor(color);
        g.fillRect(x * xScale, y * yScale, xScale-1, yScale-1);
    }

    /**
     * The field view component needs to be redisplayed. Copy the
     * internal image to screen.
     * @param g
     */
    public void paintComponent(Graphics g)
    {
        g.drawImage(fieldImage, 0, 0,gridWidth , gridHeight, null);
    }
    
    /**
     * show the atom bomb
     */
    public void AtomicBomb()
    {
    	URL url = this.getClass().getClassLoader().getResource("Images/bom.gif");
    	Image img1 = Toolkit.getDefaultToolkit().getImage(url);
    	//Image img1 = Toolkit.getDefaultToolkit().getImage("Images/bom.gif");
    	fieldImage = img1;
    	g.drawImage(fieldImage, 0, 0, gridWidth, gridHeight, this);
    }
}